package com.crazymaker.springcloud.message.controller;

import com.crazymaker.push.platform.contract.constants.PushConstants;
import com.crazymaker.springcloud.common.result.RestOut;
import com.crazymaker.springcloud.common.util.JsonUtil;
import com.crazymaker.springcloud.common.util.ThreadUtil;
import com.crazymaker.springcloud.distribute.zookeeper.ZKClient;
import com.crazymaker.springcloud.message.dao.MsgDao;
import com.crazymaker.springcloud.message.dao.MsgStreamDao;
import com.crazymaker.springcloud.message.dao.po.MsgPO;
import com.crazymaker.springcloud.message.dao.po.MsgStreamPO;
import com.crazymaker.springcloud.message.service.impl.ShardingService;
import com.crazymaker.springcloud.standard.context.SpringContextUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@RestController
@Order(Ordered.LOWEST_PRECEDENCE)
@RequestMapping("/api/crazymaker/sharding/" )
@Api(tags = "分片管理" )
public class ShardingController  implements ApplicationContextAware
{
    ShardingService shardingService;

    @Resource
    private MsgDao msgDao;
    @Resource
    private MsgStreamDao msgStreamDao;

    @PostMapping("/replaceDataSource/v1" )
    @ApiOperation(value = "调整分片" )
    public RestOut<String> replaceDataSource(@RequestBody String config)
    {
        if(!shardingService.checkConfig(config))
        {
            return RestOut.success("data error." );
        }

        shardingService.replaceDataSource(config);

        return RestOut.success("done." );
    }

    @PostMapping("/insertTestData/v1" )
    @ApiOperation(value = "插入数据做测试" )
    public RestOut<String> insertTestData(@RequestBody String count)
    {

        for (int i = 0; i < Integer.valueOf(count); i++) {
            MsgPO messagePO = new MsgPO();
            messagePO.setCreateTime(new Date());
            msgDao.saveAndFlush(messagePO);
            System.out.println("messagePO = " + JsonUtil.pojoToJson(messagePO));


            /*1 保持 明细流水*/
            MsgStreamPO streamPO = new MsgStreamPO();
            BeanUtils.copyProperties(messagePO, streamPO);
            streamPO.setMsgId(null);
            streamPO.setSendTime(new Date());
            streamPO.setTargetId("target info");
            streamPO.setReadStatus(PushConstants.PUSHED_BUT_UN_READ);
            streamPO.setSendStatus(PushConstants.PUSH_STARTING);
            streamPO.setMsgId(messagePO.getMsgId());
            streamPO.setAppKey(messagePO.getAppKey());
            msgStreamDao.saveAndFlush(streamPO);

            System.out.println("streamPO = " + JsonUtil.pojoToJson(streamPO));
        }
        return RestOut.success("done." );
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextUtil.setContext(applicationContext);
        delayDependShardingService();
    }

    private void delayDependShardingService() {
        ThreadUtil.delayRun(() -> {

            if (ZKClient.getSingleton().isReady()) {
                shardingService= SpringContextUtil.getBean(ShardingService.class);
       } else {
                delayDependShardingService();
            }
        }, ZKClient.DEPEND_DELAY, TimeUnit.SECONDS);
    }
}
